<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
	<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
	<title>
		EGF Tutorial - Pattern. First steps.
	</title>
	<link rel="stylesheet" href="../book.css" charset="ISO-8859-1" type="text/css" />

	<meta name="copyright" content="Copyright (c) Thales Corporate Services S.A.S, 2009. This page is made available under license. For full details see the LEGAL in the documentation book that contains this page." />
	<meta name="author" content="Benoit Langlois" />
	<meta name="keywords" content="egf,pattern"/>
</head>

<body xml:lang="EN-US" lang="EN-US">

<table border="0" cellpadding="2" cellspacing="0" width="100%">
  <tbody>
    <tr>
    	<td colspan="2" align="left" bgcolor="#0080c0" valign="top">
    	  <h2><font face="Arial,Helvetica"><font color="#ffffff">
    	  &nbsp;EGF Tutorial - Pattern. First steps.
    	  </font></font></h2>
    	</td>
    </tr>
	</tbody>
</table>

<p class="Para">This tutorial explains how to create and execute simple patterns with EGF. A pattern is a solution to a recurrent problem. A pattern is based on a language, for instance Jet for model-to-text transformations. The pattern technique allows pattern inheritance, delegation to other patterns in order to elaborate reusable and customizable generation solutions.</p>

<br/>

<h2 class="Head">1. Creation of pattern</h2>

<h3 class="Head">1.1. Comprehension</h3>
<p class="Para"><i>Pattern organization</i>. Patterns are organized by pattern libraries. A <b>pattern library</b> contains a set of patterns.</p>
<p class="Para"><i>Pattern nature</i>. A <b>pattern nature</b> indicates which language the pattern implementation conforms to (e.g. Java, Jet).</p>
<p class="Para"><i>Pattern views</i>. A pattern is visualized with three different folders: 1) Overview, 2) <b>Specification</b> which represents the external view of a pattern, 3) <b>Implementation</b> which represents the internal view of a pattern and where the pattern is implemented.</p>


<h3 class="Head">1.2. Practice</h3>

<p class="Para">The sequence of actions to <i>create a pattern</i>:</p>
<ul CLASS="UnorderedList">
    <li CLASS="Item">Create the <i>viewpoints</i> node - On a factory component, Commands: <i>[New Child][Viewpoint Container]</i></li>
    <li CLASS="Item">Create the <i>Pattern Viewpoint</i> node - On the <i>Viewpoint Container</i> node, Commands: <i>[New Child][Pattern Viewpoint]</i></li>
    <li CLASS="Item">Create a pattern library - On the pattern viewpoint, Commands: <i>[New Child][Library]</i>. Set the library name in order to identify it later.</li>
    <li CLASS="Item">Next, create a <i>pattern</i> - On the pattern library, Commands: <i>[New Child][JetNature Pattern | JavaNature Pattern]</i></li>
    <li CLASS="Item">Edit the pattern -  On the pattern, Commands: <i>[Double-click]</i> or <i>[Right-Click][Open]</i></li>
    <li CLASS="Item">Set the pattern name</li>
</ul>

<h3 class="Head">1.3. Application</h3>
<p class="Para">Create a pattern with the Jet nature.</p>

<p align="left">
  <img src="./images/egf_tut_pattern_1_1.png" alt="New pattern"/>
</p>
<p align="left"><i>Figure 1. Example of new pattern</i></p>  

<br/>

<h2 class="Head">2. Definition of the pattern specification</h2>

<h3 class="Head">2.1. Comprehension</h3>

<p class="Para"><i>Specification view</i>. This view enables to set 1) the super-pattern of the current pattern for pattern inheritance, 2) the pattern nature already set during the pattern creation, 3) the pattern parameters.</p>
<p class="Para"><i>Pattern query and parameter</i>. The pattern applies queries over a resource that the pattern consumes, for instance a model. Each query record is successively associated to a parameter. This record is processed by the pattern implementation.</p>
<p class="Para"><i>Pattern query</i>. EGF proposes a default query. The query list is extensible.</p>
<p class="Para"><i>Pattern parameter</i>. The pattern type is either a class defined in an ecore model or a Java type (class or interface).</p>


<h3 class="Head">2.2. Practice</h3>
<p class="Para">The sequence of actions to define <i>pattern parameters and their query</i>:</p>
<ul CLASS="UnorderedList">
    <li CLASS="Item">In the Specification view, in the Parameters section - Command: <i>[+]</i>, set the parameter name</li>
    <li CLASS="Item">Set the parameter type - Command: choose the model and a class type</li>
    <li CLASS="Item">Set the query - Command: choose the query, for instance <i>Basic query</i></li>
</ul>

<h3 class="Head">2.3. Application</h3>
<p class="Para">Create an EClass parameter. The default name of a pattern parameter is <i>parameter</i>. In the example, it is renamed <i>aClass</i>. The default is empty but it corresponds to the <i>Domain query</i>.</p>

<p align="left">
  <img src="./images/egf_tut_pattern_1_2.png" alt="Pattern Specification View"/>
</p>
<p align="left"><i>Figure 2. Pattern Specification View</i></p>  
<br/>


<h2 class="Head">3. Definition of the pattern implementation</h2>

<h3 class="Head">3.1. Comprehension</h3>

<p class="Para"><i>Implementation view</i>. This view enables to define pattern methods, the method orchestration, and local variables.</p>
<p class="Para"><i>Pattern methods</i>. Four methods are predefined: header, init, preCondition and footer. <i>Header</i> is used for instance to declare the Jet declaration or the import in Java, <i>init</i> for the initialization of local variables (cf. explanation below), <i>preCondition</i> to filter records onto a query result. The user creates as many as methods s/he wants to implement the pattern. Each pattern method contents is supposed to conform to the pattern nature, for instance Jet-based code.</p>
<p class="Para"><i>Pattern method orchestration</i>. The method orchestration defines the order to call available methods and patterns: methods local to the current pattern, methods from the super-patterns, pattern call with parameter context passing, pattern call with injection.</p>
<p class="Para"><i>Pattern variables</i>. The pattern variables are accessible by any method of the current pattern. The type is either a class defined in an ecore model or a Java type (class or interface). Pattern variables can be initialized in the <i>init</i> method, update and used in the pattern methods.</p>
<p class="Para"><i>Precondition</i>. A pattern is applied over a resource. The precondition body in written in Java, returns a boolean, and selects whether the current record for resource query is applicable or not.</p>
<p class="Para"><i>Header</i>. The header is an header for generation. In Jet, it is used to set general parameters, such the package where Java classes are generated, the imports.</p>


<h3 class="Head">3.2. Practice</h3>
<p class="Para">The sequence of actions to manage the <i>implementation methods</i>:</p>
<ul CLASS="UnorderedList">
    <li CLASS="Item">Use the palette to add/delete/rename/reorder a method.</li>
    <li CLASS="Item">To edit method, double-click on a method of the <i>Implementation methods</i> list. Next, a pattern templates editor allows editing any other method.</li>
</ul>

<p class="Para">The sequence of actions to manage <i>variables</i>:</p>
<ul CLASS="UnorderedList">
    <li CLASS="Item">In the variables, to add a variable, Command: <i>[+]</i>, set the parameter name</li>
    <li CLASS="Item">Set the parameter type - Command: click on the type and select the model / Java type</li>
</ul>


<p class="Para">The sequence of actions to manage the <i>method orchestration</i>:</p>
<ul CLASS="UnorderedList">
    <li CLASS="Item">Use the palette to add/delete/rename/reorder methods</li>
    <li CLASS="Item">Drag and drop a method from the <i>Implementation methods</i> list</li>
    <li CLASS="Item">For a super-method call, after clicking on <i>+</i>, choose the <i>method call</i> option, and next select the super-method.</li>
</ul>

<p class="Para">As explained in section 5 (Pattern translation), the pattern is translated to be executed. Save (Ctrl-s) the pattern method and the pattern to have the pattern translated.</p>


<h3 class="Head">3.3. Application</h3>
<p class="Para">Implement the default body method. Add the following Jet code:</p>
				<font color="777777"><pre>
Hello &lt;%= aClass.getName() %&gt;!
				</pre></font>
<p class="Para">Drag and drop the method in the pattern orchestration part.</p>


<br/>

<h2 class="Head">4. Configuration of the pattern execution</h2>

<h3 class="Head">4.1. Comprehension - Principle of pattern execution</h3>
<p class="Para">The following figure depicts how patterns are applied onto a domain resource.</p>

<p align="left">
  <img src="./images/egf_tut_pattern_1_3.png" alt="Pattern Execution"/>
</p>
<p align="left"><i>Figure 3. Pattern Execution</i></p>  

<p class="Para"><i>Pattern strategies</i>. A pattern strategy indicates how to navigate over a resource and how to apply patterns. For instance, a pattern-oriented strategy successively applies a set of patterns onto a resource; on the contrary, a model-oriented strategy applies a set of patterns successively onto each model element. Those two strategies are proposed by default with EGF.</p>
<p class="Para"><i>Tasks to execute patterns</i>. A task must be introduced in order to put in action all the elements described. This task must know:
  <ol CLASS="UnorderedList">
    <li> The resource applied on the patterns, for instance a model,</li>
    <li> The selection of pattern(s) or the pattern library(ies),</li>
    <li> The reporter (see below).</li>
  </ol>
</p>

<p class="Para"><i>Domain resource</i>. Patterns are applied onto a resource, such as a domain model.</p>

<p class="Para"><i>Pattern execution engines</i>. Patterns have a nature (e.g., Jet, Java). Then, at execution time, an engine executes a pattern according its nature. On the pattern flow, different pattern execution engines can be activated.</p>

<p class="Para"><i>Reporter</i>. A reporter is responsible for the rendering and persistence of the pattern results. It is typically useful for model-to-text transformation. Each pattern provides an output result that must be consolidated in one or several files. A reporter serves to report the result of the pattern implementation, for instance on the console or in a file.</p>
<br/>

<h3 class="Head">4.2. Practice</h3>
<p class="Para">A task is used to execute a pattern or a set of patterns. This task must know: 1) the domain model, 2) the pattern or pattern library list, 3) the pattern strategy, and 4) the reporter. This tutorial uses the <i>Model-driven pattern strategy</i>. The use of pattern reporter is explained below.</p>
<p class="Para">The sequence of actions to create a task for pattern execution:</p>
<ul CLASS="UnorderedList">
    <li CLASS="Item"><i>Domain creation</i>. This step can be reused independently of the pattern usage.</li>
    <ul CLASS="UnorderedList">
            <li CLASS="Item">Creation of the domain viewpoint where domains are declared - On the Viewpoint Container, Command: [New Child][Domain Viewpoint]</li>
            <li CLASS="Item">Creation of a domain reference - On the Domain Viewpoint, for EMF, Command: [New Child][EMF Domain]. Next, in the Uri property, providing an ecore Uri like <i>platform:/plugin/org.eclipse.egf.model/model/Fcore.ecore</i></li>
    </ul>
    <br/>
    <li CLASS="Item"><i>Configuration of the pattern call</i>.</li>
    <ul CLASS="UnorderedList">
        <li CLASS="Item">Create of production plan in the factory component - On the Factory Component, Command: [New Child][Production Plan] 
        </li>
        <li CLASS="Item">Creation of the pattern execution task - On the Production Plan node, Command: [Right-Click][Invoke Activity], and choose the <i>Domain driven pattern strategy task [Task Java]</i></li>
        <li CLASS="Item">Click on <i>Next</i> and select only <i>domain</i> and <i>pattern.ids</i>. Click on <i>Finish</i>.</li>
        <li CLASS="Item">Set the domain - On the new Activity / Invocation Contract Container / domain Invocation Contract, Command: 1) [Right-Click][New Child][Type Domain], 2) Set the <i>Domain</i> property of this Domain URI with domain URI declared in the Domain viewpoint</li>
        <li CLASS="Item">Set the pattern library - On the new Activity / Invocation Contract Container / pattern.ids Invocation Contract, Command: [New Child][Type Pattern List]; Next, click on the [Elements] property, select the pattern library newly created</li>
    </ul>
</ul>

<br/>


<h3 class="Head">4.3. Application</h3>
<p class="Para">Create a pattern task invocation as described above. Example of domain: <i>platform:/plugin/org.eclipse.egf.model/model/Fcore.ecore</i>.</p>

<p class="Para">Finally, execute the factory component. The output is displayed on the console. The last line sums up all the previous lines.</p>

<br/>


<h3 class="Head">4.4. Fixing pattern method errors</h3>
<p class="Para">Patterns to be compiled are compiled when a pattern is saved. Errors may be raised and, as a consequence, the pattern execution would fail. Then:</p>
<ul CLASS="UnorderedList">
    <li CLASS="Item">At anytime, it is possible to regenerate all pattern classes and methods: right-click on the Pattern viewpoint, and apply the [Generate Patterns] command on the Pattern viewpoint.</li>
    <li CLASS="Item">Fixing the errors. In the Eclipse Package Explorer, locate the errors in the generated methods, update the pattern methods accordingly, and save the pattern methods.</li>
</ul>

<br/>

<h3 class="Head">4.5. Comprehension - Pattern Reporter</h3>
<p class="Para"><i>Reporter class</i>. A reporter is a Java class which implements the PatternExecutionReporter class.</p>
<p class="Para"><i>Collecting the result</i>. There are two methods to collect the execution result of one pattern: 1) the <i>loopFinished</i> method collects the result for one pattern and for one query tuple, 2) the <i>executionFinished</i> method collects the result for one pattern, which is the aggregation of the <i>loopFinished</i> method results.</p>
<p class="Para"><i>Reporting the result</i>. The result is generally reported in with the selected media, e.g. a file.</p>

<p class="Para">For more complex reports, refer to the <i>org.eclipse.egf.emf.docgen.html</i> plug-in which examplifies the case of result in a file and a specific folder.</p>

<br/>

<h3 class="Head">4.6. Practice</h3>

<p class="Para">1. Create a reporter Java class like this one:</p>
				<font color="777777"><pre>
Package org.eclipse.egf.examples.uc1.reporter;

import java.util.Map;

import org.eclipse.egf.core.EGFCorePlugin;
import org.eclipse.egf.model.pattern.PatternContext;
import org.eclipse.egf.model.pattern.PatternExecutionReporter;

public class MyReporter implements PatternExecutionReporter{

	public void executionFinished(String output, PatternContext context) {
		// Message on the default console
		System.out.println("=> Result of pattern:\n"+output);
		// Message on the EGF console
		EGFCorePlugin.getDefault().logInfo("=> Result of pattern:\n"+output);
	}

	public void loopFinished(String output, String outputWithCallBack,
			PatternContext context, Map&lt;String, Object&gt; parameterValues) {
		// TODO Auto-generated method stub
		
	}

}
				</pre></font>

<p class="Para">2. Complete the task with the reporter parameter:
    <ul CLASS="UnorderedList">
        <li CLASS="Item">Add a contract to the task created before - Command: on the Inovation Contract Container, [New Child][Invocation Contract]</li>
        <li CLASS="Item">In <i>Behaviour/Invoked Contract</i> property, select the <i>pattern.execution.reporter</i></li>
        <li CLASS="Item">Add the <i>Type Pattern Execution Reporter</i> - Command: on the Invocation Contract, [New Child][Type Pattern Execution Reporter], and select the pattern reporter class in the <i>value</i> property</li>
        <li CLASS="Item">Execute the factory component to see the new result</li>
    </ul>
</p>

<h2 class="Head">5. Pattern translation</h2>
<p class="Para">The pattern contents is translated into an executable form. This takes into account pattern inheritance and other pattern mechanisms not explained in this tutorial (e.g., pattern call). Next, at execution time, a pattern engine, specific to the pattern nature, executes a pattern.</p>
<p class="Para">Patterns are translated on the fly. One case requires to manually force pattern rebuild: pattern inheritance. Indeed, children are not notified that a super-pattern in the hierarchy is update. To force this translation:</p>

<ul CLASS="UnorderedList">
    <li CLASS="Item">On the <i>Pattern viewpoint</i> which contains all the pattern libraries - Command: <i>[Right-Click][Generate Patterns]</i> </li>
</ul>


</body>
</html>
